package com.cskaoyan.service.wx;

import com.cskaoyan.bean.goods.GoodsExample;
import com.cskaoyan.bean.mallbean.Category;
import com.cskaoyan.bean.mallbean.CategoryExample;
import com.cskaoyan.bean.promotebean.*;
import com.cskaoyan.mapper.goodsmapper.GoodsMapper;
import com.cskaoyan.mapper.mallmapper.CategoryMapper;
import com.cskaoyan.mapper.promotemapper.AdMapper;
import com.cskaoyan.mapper.promotemapper.Groupon_rulesMapper;
import com.cskaoyan.mapper.promotemapper.TopicMapper;
import com.cskaoyan.mapper.wx.WxHomeIndexMapper;
import com.cskaoyan.vo.wx.*;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hsp
 * @date 2021/5/12 21:55
 */
@Service
public class WxHomeIndexServiceImpl implements WxHomeIndexService {
    @Autowired
    AdMapper adMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    WxHomeIndexMapper wxHomeIndexMapper;
    @Autowired
    Groupon_rulesMapper groupon_rulesMapper;
    @Autowired
    TopicMapper topicMapper;

    @Override
    public WxHomeIndexVo getHomeIndex() {
        //第一个list
//            private List<NewGoodsListBean> newGoodsList;
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.createCriteria().andDeletedEqualTo(false).andIsNewEqualTo(true);
        List<WxHomeIndexVoSimpleGoods> newGoodsList = goodsMapper.selectSimpleGoodsByExample(goodsExample);
//      第二个list     private List<WxhomeIndexVoCouponListBean > couponList;
        List<WxhomeIndexVoCouponListBean> couponList = wxHomeIndexMapper.selectSimpleCouponList();
//        第三个list      private List<ChannelBean> channel;
        List<WxHomeIndexVoChannelBean> channel = categoryMapper.selectSimpleCategory();
//        第四个list     private List<WxHomeIndexVoGrouponList> grouponList;
        List<WxHomeIndexVoGrouponList> grouponList = getGrouponList();
        // 第五个list  private List<BannerBean> banner;
//        List<WxHomeIndexVo.BannerBean> bannerBeans = adMapper.selectByExample(new );
        AdExample adExample = new AdExample();
        adExample.createCriteria().andDeletedEqualTo(false);
        List<Ad> banners = adMapper.selectByExample(adExample);
//        第六个list    private List<WxhomeIndexVoBrandListBean> brandList;
        List<WxhomeIndexVoBrandListBean> beanList = wxHomeIndexMapper.selectSimpleBrandBean();
//        第七个list     private List<Goods> hotGoodsList;
        GoodsExample goodsExample2 = new GoodsExample();
        goodsExample2.createCriteria().andDeletedEqualTo(false).andIsHotEqualTo(true);
        List<WxHomeIndexVoSimpleGoods> hotGoodsList = goodsMapper.selectSimpleGoodsByExample(goodsExample2);
//        第八个List     private List<WxhomeIndexVoTopicListBean> topicList;
        TopicExample topicExample = new TopicExample();
        PageHelper.startPage(0, 10);
        topicExample.createCriteria().andDeletedEqualTo(false);
        List<WxhomeIndexVoTopicListBean> topicList = topicMapper.selectSimpleTopicByExample(topicExample);
//      第九个list List<WxhomeIndexVoFloorGoodsListBean> floorGoodsList;
        List<WxhomeIndexVoFloorGoodsListBean> floorGoodsList = getWxhomeIndexVoFloorGoodsListBean();
        WxHomeIndexVo wxHomeIndexVo = new WxHomeIndexVo(newGoodsList, couponList, channel, grouponList,banners, beanList, hotGoodsList, topicList, floorGoodsList);
        return wxHomeIndexVo;
    }

    private List<WxhomeIndexVoFloorGoodsListBean> getWxhomeIndexVoFloorGoodsListBean() {
        List<WxhomeIndexVoFloorGoodsListBean> floorGoodsList = new ArrayList<>();

        CategoryExample example = new CategoryExample();
        example.createCriteria().andDeletedEqualTo(0).andLevelEqualTo("L1");
        List<Category> categories = categoryMapper.selectByExample(example);
        for (Category category : categories) {
            String name = category.getName();
            Integer id = category.getId();
            List<WxHomeIndexVoSimpleGoods> goodsList = getGoodListByCategoryId(id);
            WxhomeIndexVoFloorGoodsListBean tempWxhomeIndexVoFloorGoodsListBean = new WxhomeIndexVoFloorGoodsListBean();
            tempWxhomeIndexVoFloorGoodsListBean.setName(name);
            tempWxhomeIndexVoFloorGoodsListBean.setId(id);
            tempWxhomeIndexVoFloorGoodsListBean.setGoodsList(goodsList);

            floorGoodsList.add(tempWxhomeIndexVoFloorGoodsListBean);
        }

        return floorGoodsList;

    }

    private List<WxHomeIndexVoSimpleGoods> getGoodListByCategoryId(Integer categoryId) {

        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andPidEqualTo(categoryId);
        List<Integer> integers = new ArrayList<>();
        List<Category> categories = categoryMapper.selectByExample(categoryExample);

        for (Category category : categories) {
            Integer id = category.getId();
            integers.add(id);
        }
        GoodsExample goodsExample = new GoodsExample();
        PageHelper.startPage(0,10);
        goodsExample.createCriteria().andDeletedEqualTo(false).andCategoryIdIn(integers);
        List<WxHomeIndexVoSimpleGoods> goodsList = goodsMapper.selectSimpleGoodsByExample(goodsExample);
        return goodsList;
    }

    private List<WxHomeIndexVoGrouponList> getGrouponList() {

        List<WxHomeIndexVoGrouponList> grouponList = new ArrayList<>();
        WxHomeIndexVoGrouponList wxHomeIndexVoGrouponList = new WxHomeIndexVoGrouponList();
        Groupon_rulesExample groupon_rulesExample = new Groupon_rulesExample();
        groupon_rulesExample.createCriteria().andDeletedEqualTo(false);
        List<Groupon_rules> groupon_rules = groupon_rulesMapper.selectByExample(groupon_rulesExample);
        for (Groupon_rules groupon_rule : groupon_rules) {
            int groupon_price = groupon_rule.getDiscount().intValue();
            int group_member = groupon_rule.getDiscountMember().intValue();
            WxHomeIndexVoGrouponListGoods goods = wxHomeIndexMapper.selectSimpleGoodsById(groupon_rule.getGoodsId());

            wxHomeIndexVoGrouponList.setGroupon_price(groupon_price);
            wxHomeIndexVoGrouponList.setGroupon_member(group_member);
            wxHomeIndexVoGrouponList.setGoods(goods);
            grouponList.add(wxHomeIndexVoGrouponList);
        }
        return grouponList;
    }
}
